let requestUrl;

function addHandler() {
	requestUrl = "/account/insert";

	$("#account_dialog").dialog("open");
}

function editHandler() {
	let rowData = $("#account_list").datagrid("getSelected");
	
	if (rowData) {
		requestUrl = "/account/updateById";

		$("#id").val(rowData.id);
		$("#phone").textbox("setValue", rowData.phone);
		$("#account").textbox("setValue", rowData.account);

		$("#account_dialog").dialog("open");
	} else {
		alertMsg("请选择要修改的记录！", "warning");
	}
}

function deleteHandler() {
	let rowData = $("#account_list").datagrid("getSelected");
	
	if (rowData) {
		$.messager.confirm("确认", "是否删除账号？", function(bool) {
			if (bool) {
				ajaxPost("/account/deleteById", {
					id: rowData.id
				}, function(response) {
					showMsg(response.message);

					$("#account_list").datagrid("reload");
				}, error);
			}
		});
	} else {
		alertMsg("请选择要删除的记录！", "warning");
	}
}

$(document).ready(function() {

    $("#searchbox").searchbox({
    	width: 200,
    	prompt: "请输入正确的手机号",
        searcher: function(keyword){
    		$("#account_list").datagrid("load", {
    			phone: keyword
    		});
        }
    });
    
	$("#account").textbox({
		width: 160,
		required: true
	});
	
	$("#phone").textbox({
		width: 160,
		required: true
	});
	
	$("#account_dialog").dialog({
		title: "账号信息",
		width: 500,
		height: 300,
		buttons: [{
			iconCls: "icon-save",
			text: "保存",
			handler: function() {
				let selector = "#account_form";

				checkForm(selector, function () {
					// 验证账号
					let account = $("#account").textbox("getValue");

					// 邮箱账号或者手机号
					if (isPhone(account) || isEmail(account)) {
						// 验证手机号
						let phone = $("#phone").textbox("getValue");

						if (isPhone(phone)) {
							let data = $(selector).serialize();

							ajaxPost(requestUrl, data, function(response) {
								showMsg(response.message);

								$(selector).form("clear");
								$("#account_dialog").dialog("close");
								$("#account_list").datagrid("reload");
							}, error);
						} else {
							alertMsg("请输入正确的手机号！", "warning");
						}
					} else if (isNumber(account)) {
						alertMsg("请输入正确的手机号！", "warning");
					} else {
						alertMsg("请输入正确的邮箱！", "warning");
					}
				});
			}
		}, {
			iconCls: "icon-cancel",
			text: "取消",
			handler: function() {
				$("#account_dialog").dialog("close");
				$("#account_form").form("clear");
			}
		}]
	});
	
	$("#account_list").datagrid({
		url: "/account/selectByPage",
		title: "账号列表",
		height: table_height_with_search,
		rownumbers: true,
		pagination: true,
		toolbar: [{
			iconCls: "icon-add",
			text: "添加",
			handler: function() {
				addHandler();
			}
		}, "-", {
			iconCls: "icon-edit",
			text: "修改",
			handler: function() {
				editHandler();
			}
		}, "-", {
			iconCls: "icon-delete",
			text: "删除",
			handler: function() {
				deleteHandler();
			}
		}],
		columns: [[
			{field: "account", title: "账号", align:"center", width: 100},
			{field: "password", title: "密码", align:"center", width: 100
				, formatter: function() {
					return "<div>*****</div>";
				}
			}, // 伪列
			{field: "registerTime", title: "注册时间", align:"center", width: 100
				, formatter: function() {
					return "<div>*****</div>";
				}
			}, // 伪列
			{field: "phone", title: "绑定手机号", align:"center", width: 100
				, formatter: function(value) {
					return "<div>" + value.substr(0, 3) + "****" + value.substr(7) + "</div>";
				}
			}
		]]
	});

});